import { NextResponse } from "next/server"
import OpenAI from "openai"

export async function POST(req: Request) {
  const { 描述 } = await req.json()
  const apiKey = req.headers.get("X-API-Key")

  if (!apiKey) {
    return NextResponse.json({ error: "API 密钥未提供" }, { status: 401 })
  }

  const openai = new OpenAI({
    apiKey: apiKey,
  })

  try {
    const response = await openai.images.generate({
      model: "dall-e-3",
      prompt: `生成一张高质量的食物图片，描述如下：${描述}。图片应该看起来美味可口，适合用于餐厅菜单。`,
      n: 1,
      size: "1024x1024",
    })

    return NextResponse.json({ 图片链接: response.data[0].url })
  } catch (error) {
    console.error("图片生成出错:", error)
    return NextResponse.json({ error: "图片生成失败" }, { status: 500 })
  }
}

